package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"strings"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/dialog"
	"fyne.io/fyne/v2/widget"
	"github.com/flopp/go-findfont"
)

func init() {
	//设置中文字体:解决中文乱码问题
	fontPaths := findfont.List()
	for _, path := range fontPaths {
		if strings.Contains(path, "msyh.ttf") || strings.Contains(path, "simhei.ttf") ||
			strings.Contains(path, "simsun.ttc") || strings.Contains(path, "simkai.ttf") {
			os.Setenv("FYNE_FONT", path)
			break
		}
	}
}

const nginxConfigFilePath = "./nginx/conf/nginx.conf"

func readNginxConfig() string {
	content, err := ioutil.ReadFile(nginxConfigFilePath)
	if err != nil {
		log.Fatal("Error reading file:", err)
	}
	return string(content)
}

func saveNginxConfig(content string) error {
	stopNginx()
	err := ioutil.WriteFile(nginxConfigFilePath, []byte(content), 0644)
	if err != nil {
		log.Fatal("Error writing file:", err)
		return err
	}
	fmt.Println("Nginx配置已保存")
	return nil
}

// 启动Nginx
func startNginx() error {
	stopNginx()
	return runNginxBat("start.bat")
}

// 关闭Nginx
func stopNginx() error {
	return runNginxBat("stop.bat")
}

func testNginx() error {
	stopNginx()
	return runNginxBat("test.bat")
}

func runNginxBat(name string) error {
	// 获取当前目录路径
	currentDir, err := os.Getwd()
	if err != nil {
		fmt.Println(err)
		return err
	}

	// 构建bat文件路径
	batPath := filepath.Join(currentDir, "nginx", name)

	// 构建执行命令
	cmd := exec.Command("cmd", "/C", batPath)

	// 设置命令执行的工作目录
	cmd.Dir = filepath.Join(currentDir, "nginx")

	// 执行命令
	err = cmd.Run()
	return err
}

func main() {
	myApp := app.New()
	window := myApp.NewWindow("Nginx配置器")
	window.Resize(fyne.NewSize(600, 600))

	// 读取初始Nginx配置
	configContent := readNginxConfig()

	// 创建用于显示
	nginxConfEntry := widget.NewMultiLineEntry()
	nginxConfEntry.SetMinRowsVisible(27)
	nginxConfEntry.Resize(fyne.NewSize(600, 600))
	nginxConfEntry.SetText(configContent)

	// 创建启动和关闭按钮
	startButton := widget.NewButton("启动", func() {
		err := startNginx()
		if err != nil {
			log.Println("启动Nginx失败:", err)
			dialog.ShowError(err, window)
		} else {
			log.Println("Nginx已启动")
			dialog.ShowInformation("提示", "完成", window)
		}
	})

	stopButton := widget.NewButton("关闭", func() {
		err := stopNginx()
		if err != nil {
			log.Println("关闭Nginx失败:", err)
			dialog.ShowError(err, window)
		} else {
			log.Println("Nginx已关闭")
			dialog.ShowInformation("提示", "完成", window)
		}
	})

	saveButton := widget.NewButton("更新", func() {
		err := saveNginxConfig(nginxConfEntry.Text)
		if err != nil {
			log.Println("更新配置失败:", err)
			dialog.ShowError(err, window)
		} else {
			log.Println("更新配置完成")
			dialog.ShowInformation("提示", "完成", window)
		}
	})

	testButton := widget.NewButton("测试", func() {
		err := testNginx()
		if err != nil {
			log.Println("测试Nginx失败:", err)
			dialog.ShowError(err, window)
		} else {
			log.Println("测试Nginx完成")
			dialog.ShowInformation("提示", "完成", window)
		}

	})

	// 创建布局
	content := container.NewVBox(
		widget.NewLabel("Nginx配置"),
		nginxConfEntry,
		container.NewGridWithColumns(4,
			saveButton,
			testButton,
			startButton,
			stopButton,
		),
	)

	window.SetContent(content)
	window.CenterOnScreen() // 将窗口居中显示
	window.ShowAndRun()
}
